#include "Main.h"

unsigned int o_MouseX = 0.0f;
unsigned int o_MouseY = 0.0f;



GameInput::GameInput(){
	/*
	f_mouse_sensitivity = 0.1f;
	f_walk_velocity = 0.01f;
	o_MouseX=0;
	o_MouseY=0;
	arrCamPosition[X] = -3.0f;
	arrCamPosition[Y] = -10.0f;
	arrCamPosition[Z] = -8.0f;
	*/
}


GameInput::~GameInput(){

}

void GameInput::GetInputs(){

	int delta_MouseX = 0;
	int delta_MouseY = 0;
	const sf::Input& Input = App.GetInput();
	unsigned int MouseX = Input.GetMouseX();
	unsigned int MouseY = Input.GetMouseY();
	if(MouseX == 0 && MouseY == 0){
		MouseX=320;
		MouseY=240;
	}

	//reset degree
	if(arrViewAngles[YAW] > 360) arrViewAngles[YAW]-=360;
	if(arrViewAngles[YAW] <0) arrViewAngles[YAW] +=360;

	delta_MouseX = 320-MouseX;
	delta_MouseY = 240-MouseY;


	char buffer[6];
	char strCamCoordinates[255];
	itoa(arrCamPosition[X],buffer,10);
	strcpy(strCamCoordinates, buffer);
	itoa(arrCamPosition[Z],buffer,10);
	strcat(strCamCoordinates, ",");
	strcat(strCamCoordinates, buffer);
	itoa(arrViewAngles[YAW],buffer,10);
	strcat(strCamCoordinates, ":");
	strcat(strCamCoordinates, buffer);
	strcat(strCamCoordinates, " deg");
	strcat(strCamCoordinates, "\nSphere Position:");

	float spherePosition_x = arrCamPosition[X]-(10.0f*sin((arrViewAngles[YAW])*(PI/180)));
	float spherePosition_y = 0.0f;
	float spherePosition_z = arrCamPosition[Z]+(10.0f*cos((arrViewAngles[YAW])*(PI/180)));

	itoa(spherePosition_x,buffer,10);
	strcat(strCamCoordinates, buffer);
	strcat(strCamCoordinates, ",");
	itoa(spherePosition_y,buffer,10);
	strcat(strCamCoordinates, buffer);
	strcat(strCamCoordinates, ",");
	itoa(spherePosition_z,buffer,10);
	strcat(strCamCoordinates, buffer);
	strcat(strCamCoordinates, "\n");
	Text.SetText(strCamCoordinates);

	
	o_MouseX = MouseX;
	o_MouseY = MouseY;
	Vector VelocityVector;
	VelocityVector.x = 0;
	VelocityVector.y = 0;
	VelocityVector.z = 0;
	if(Input.IsKeyDown(sf::Key::W)){
		VelocityVector.x = f_walk_velocity*sin((-arrViewAngles[YAW])*(PI/180));
		VelocityVector.y = 0;
		VelocityVector.z = f_walk_velocity*cos((-arrViewAngles[YAW])*(PI/180));

	}
	if(Input.IsKeyDown(sf::Key::Escape)){
		App.Close();
	}
	if(Input.IsKeyDown(sf::Key::S)){
		VelocityVector.x = f_walk_velocity*sin((180-arrViewAngles[YAW])*(PI/180));
		VelocityVector.y = 0;
		VelocityVector.z = f_walk_velocity*cos((180-arrViewAngles[YAW])*(PI/180));
		//Collision::CheckForCollision(&VelocityVector);

		//arrCamPosition[X] += f_walk_velocity*sin((180-arrViewAngles[YAW])*(PI/180));
		//arrCamPosition[Z] += f_walk_velocity*cos((180-arrViewAngles[YAW])*(PI/180));
		
	}
	if(Input.IsKeyDown(sf::Key::A)){
		VelocityVector.x = f_walk_velocity*sin((90-arrViewAngles[YAW])*(PI/180));
		VelocityVector.y = 0;
		VelocityVector.z = f_walk_velocity*cos((90-arrViewAngles[YAW])*(PI/180));
		//if(!Collision::CheckForCollision(&VelocityVector)){
		//	arrCamPosition[X] += f_walk_velocity*sin((90-arrViewAngles[YAW])*(PI/180));
		//	arrCamPosition[Z] += f_walk_velocity*cos((90-arrViewAngles[YAW])*(PI/180));
		//}
	}
	if(Input.IsKeyDown(sf::Key::D)){
		VelocityVector.x = f_walk_velocity*sin((270-arrViewAngles[YAW])*(PI/180));
		VelocityVector.y = 0;
		VelocityVector.z = f_walk_velocity*cos((270-arrViewAngles[YAW])*(PI/180));
		//if(!Collision::CheckForCollision(&VelocityVector)){
		//	arrCamPosition[X] += f_walk_velocity*sin((270-arrViewAngles[YAW])*(PI/180));
		//	arrCamPosition[Z] += f_walk_velocity*cos((270-arrViewAngles[YAW])*(PI/180));
		//}
	}
	Vector Center;
	Center.x = -arrCamPosition[X];
	Center.y = -arrCamPosition[Y];
	Center.z = -arrCamPosition[Z];
	


	bool bCollided= Collision::CollideEllipsoid(Center,VelocityVector);

	arrCamPosition[X] += VelocityVector.x;
	arrCamPosition[Y] += VelocityVector.y;
	arrCamPosition[Z] += VelocityVector.z;
	
	//Gravity
	//VelocityVector.y = 0.1f;
	//Collision::CheckForCollision(&VelocityVector);
	//arrCamPosition[Y] += VelocityVector.y;

	if(Input.IsKeyDown(sf::Key::Up)){
		arrViewAngles[PITCH]-=0.1f;
	}
	if(Input.IsKeyDown(sf::Key::Down)){
		arrViewAngles[PITCH]+=0.1f;
	}
	if(Input.IsKeyDown(sf::Key::Left)){
		arrViewAngles[YAW]-=0.1f;
	}
		if(Input.IsKeyDown(sf::Key::Right)){
		arrViewAngles[YAW]+=0.1f;
	}
	
	if(delta_MouseX!=0){
		arrViewAngles[YAW] -= delta_MouseX*f_mouse_sensitivity;
	}
	if(delta_MouseY!=0){
		arrViewAngles[PITCH] -= delta_MouseY*f_mouse_sensitivity;
	}
	
}